package com.geekjob1120.geekjob1120pay;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.scheduler.Schedulers;

import java.math.BigDecimal;
import java.util.concurrent.CountDownLatch;

@Component
@Slf4j
public class DemoCommandRunner implements ApplicationRunner {

//    @Autowired
    PayFeignClient payFeignClient;
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    WebClient webClient;

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        testFeign();
        testRestTemplate();
        testWebClient();
    }

    private void testWebClient() throws InterruptedException {
        log.info("开始使用webclient");
        CountDownLatch countDownLatch = new CountDownLatch(2);
        PayServiceIO in = PayServiceIO.builder().payway("1")
                .orderId("w1").amt(BigDecimal.valueOf(23.5)).prodDesc("测试w1XML").build();

        webClient.post().uri("/pay/payService")
                .accept(MediaType.APPLICATION_JSON)
                .body(in,PayServiceIO.class)
                .retrieve()
                .bodyToMono(PayServiceIO.class)
                .doFinally(s->countDownLatch.countDown())
                .subscribeOn(Schedulers.single())
                .subscribe(c->log.info("返回订单对象：{}",c));
        log.info("开启查询请求");
        webClient.get().uri("/payOrder/{orderId}",in.getOrderId())
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .bodyToMono(String.class)
                .doOnError(t->log.error("请求报错",t))
                .doFinally(s->countDownLatch.countDown())
                .subscribeOn(Schedulers.single())
                .subscribe(c->log.info("获取到响应报文体:{}",c));
        countDownLatch.await();

    }

    private void testRestTemplate() {
        log.info("开始使用 restTemplate");
        PayServiceIO in = PayServiceIO.builder().payway("2")
                .orderId("r1").amt(BigDecimal.valueOf(23.5)).prodDesc("测试r1XML").build();
        PayServiceIO out = restTemplate.postForObject("http://localhost:18808/pay/payService",
                in,PayServiceIO.class);
    }

    private void testFeign(){
        log.info("testFeign开始");
        PayServiceIO in = PayServiceIO.builder().payway("3")
                .orderId("f1").amt(BigDecimal.valueOf(23.5)).prodDesc("测试1XML").build();
        PayServiceIO out = payFeignClient.payServiceByXMl(in);
        log.info("out:{}",out);
        PayServiceIO find1 = payFeignClient.searchOrderByXMl(in.getOrderId());
        log.info("find1:{}",find1);
        PayServiceIO in2 = PayServiceIO.builder().payway("3")
                .orderId("j1").amt(BigDecimal.valueOf(23.5)).prodDesc("测试1XML").build();
        PayServiceIO out2 = payFeignClient.payServiceByXMl(in2);
        log.info("out:{}",out2);
        PayServiceIO find2 = payFeignClient.searchOrderByXMl(in2.getOrderId());
        log.info("find1:{}",find2);
    }

}
